﻿using System;
using System.Collections.Generic;
using System.Linq;
using Books.Interfaces;
using Books.Model;
using RefLib.Data;
using RefLib.MVC.JsonModels;

namespace Books.App.Publisher
{
    public class PublisherApp : IPublisherApp
    {
        public PublisherApp(IBooksDataService booksDataService)
        {
            if (booksDataService == null)
                throw new Exception("IBooksDataService is null");
            _booksDataService = booksDataService;
        }

        public void DeletePublisher(int publisherId)
        {
            _booksDataService.DeletePublisher(publisherId);
        }

        public PublisherModel GetPublisherById(int publisherId)
        {
            return _booksDataService.GetPublisherById(publisherId);
        }

        public FlexigridJsonModel.Response GetPublishers(FlexigridJsonModel.Request context)
        {
            PublisherModel[] sortedPublishers = _booksDataService.GetAllPublishers(context.SortCriteria);
            var pagedPublishers = new Pager<PublisherModel>().Page(context.page, context.rp, sortedPublishers);

            return new FlexigridJsonModel.Response()
                {
                    page = context.page,
                    rows = MapFromModel(pagedPublishers).ToList(),
                    total = sortedPublishers.Length
                };
        }

        public PublisherModel[] MatchPublishers(string publisherName, string city)
        {
            return _booksDataService.MatchPublishers(publisherName, city);
        }

        public int WritePublisher(PublisherModel model)
        {
            if (model.PublisherId == 0)
                return _booksDataService.CreatePublisher(model);
            else
            {
                _booksDataService.UpdatePublisher(model);
                return model.PublisherId;
            }
        }

        private ICollection<FlexigridJsonModel.Response.Row> MapFromModel(IEnumerable<PublisherModel> collection)
        {
            var rowList = new List<FlexigridJsonModel.Response.Row>();
            var enumerator = collection.GetEnumerator();
            while (enumerator.MoveNext())
            {
                rowList.Add(MapFromModel(enumerator.Current));
            }
            return rowList;
        }

        private FlexigridJsonModel.Response.Row MapFromModel(PublisherModel model)
        {
            var row = new FlexigridJsonModel.Response.Row()
                {
                    id = model.PublisherId.ToString(),
                    cell = new
                        { 
                            AuthorCount = model.AuthorCount,
                            BookCount = model.BookCount,
                            City = model.City,
                            PublisherId = model.PublisherId,
                            PublisherName = model.PublisherName
                        }
                };

            return row;
        }

        private readonly IBooksDataService _booksDataService;
    }
}
